﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SlimCode.SlimPasswords
{
  abstract class LaunchableItem
  {
    public LaunchableItem()
    {
    }

    public LaunchableItem( string name )
    {
    }

    #region Active PROPERTY

    private bool m_active = true;

    public bool Active
    {
      get { return m_active; }
      set
      {
        if( value != m_active )
        {
          m_active = value;
          this.OnActiveChanged();
        }
      }
    }

    public event EventHandler ActiveChanged;

    protected virtual void OnActiveChanged()
    {
      EventHandler handlers = this.ActiveChanged;

      if( handlers != null )
      {
        handlers( this, EventArgs.Empty );
      }
    }

    #endregion

    #region Description PROPERTY

    public abstract string Description { get; }

    #endregion

    #region LastCommand PROPERTY

    private string m_lastCommand = string.Empty;

    public string LastCommand
    {
      get { return m_lastCommand; }
      set
      {
        if( value == null )
          value = string.Empty;

        if( value != m_lastCommand )
        {
          m_lastCommand = value;
          this.OnLastCommandChanged();
        }
      }
    }

    public event EventHandler LastCommandChanged;

    protected virtual void OnLastCommandChanged()
    {
      EventHandler handlers = this.LastCommandChanged;

      if( handlers != null )
      {
        handlers( this, EventArgs.Empty );
      }
    }

    #endregion

    #region Name PROPERTY

    public abstract string Name { get; }

    #endregion

    #region Title PROPERTY

    private string m_title = string.Empty;

    public string Title
    {
      get { return m_title; }
      set
      {
        if( value == null )
          value = string.Empty;

        if( value != m_title )
        {
          m_title = value;
          this.OnTitleChanged();
        }
      }
    }

    public event EventHandler TitleChanged;

    protected virtual void OnTitleChanged()
    {
      EventHandler handlers = this.TitleChanged;

      if( handlers != null )
      {
        handlers( this, EventArgs.Empty );
      }
    }

    #endregion

    public virtual bool Load( XmlNode parentNode )
    {
      try
      {
        XmlNode valueNode = parentNode.SelectSingleNode( "LastLaunchCommand" );

        if( valueNode != null )
        {
          this.LastCommand = valueNode.InnerText;
        }

        valueNode = parentNode.SelectSingleNode( "Active" );

        if( valueNode != null )
        {
          try
          {
            this.Active = XmlConvert.ToBoolean( valueNode.InnerText );
          }
          catch 
          {
            try
            {
              this.Active = bool.Parse( valueNode.InnerText );
            }
            catch { }
          }
        }

        valueNode = parentNode.SelectSingleNode( "Title" );

        if( valueNode == null )
        {
          // Old format. Use the name.
          valueNode = parentNode.SelectSingleNode( "Name" );

          if( valueNode != null )
          {
            this.Title = valueNode.InnerText;
          }
        }
        else
        {
          this.Title = valueNode.InnerText;
        }
      }
      catch
      {
        return false;
      }

      return true;
    }

    public virtual void Save( XmlWriter writer )
    {
      // We do not save the "Name" anymore. It it always constructed out of the category and title.

      if( !string.IsNullOrEmpty( this.LastCommand ) )
      {
        writer.WriteElementString( "LastLaunchCommand", this.LastCommand );
      }

      writer.WriteElementString( "Active", XmlConvert.ToString( this.Active ) );
      writer.WriteElementString( "Title", this.Title );
    }
  }
}
